package com.xbongbong.crm.service.impl;

import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.FormDataHelp;
import com.xbongbong.paas.pojo.dto.SubFormListDTO;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.helper.PageHelper;
import com.xbongbong.parent.abstracts.AbstractBusinessSubFormListService;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/11/19 11:21
 */
@Service("contractSubFormListService")
public class ContractSubFormListService extends AbstractBusinessSubFormListService<PaasFormDataEntityExt> {

    @Resource
    private FormDataHelp formDataHelp ;

    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.CONTRACT.getCode());
    }

    @Override
    public PaasFormDataEntityExt getFormData(SubFormListDTO subFormListDTO) throws XbbException {
        PaasFormDataEntityExt paasFormDataEntityExt = formDataHelp.getFormData(subFormListDTO.getDataId(), subFormListDTO.getCorpid(), subFormListDTO.getBusinessType());

        return paasFormDataEntityExt;
    }



    @Override
    public PaasFormDataEsListVO dealData(SubFormListDTO subFormListDTO, PaasFormDataEntityExt formDataEntity, Map<String, FieldAttrEntity> explainMap) throws XbbException {

        Long dataId = subFormListDTO.getDataId();
        List<FieldAttrEntity> subExplainList = getSubExplainList(subFormListDTO, explainMap);
        PaasFormDataEsListVO formDataListVO = new PaasFormDataEsListVO();
        PageHelper pageHelper = new PageHelper();
        FieldAttrEntity fieldAttrEntity = explainMap.get(subFormListDTO.getAttr());
        if (noHasPermission(subFormListDTO, fieldAttrEntity, formDataEntity.getCreatorId())) {
            formDataListVO.setPageHelper(pageHelper);
            formDataListVO.setPaasFormDataESList(new ArrayList<>());
            formDataListVO.setHeadList(subExplainList);
            return formDataListVO;
        }

        if (Objects.equals(FieldTypeEnum.SUB_FORM.getType(), fieldAttrEntity.getAttr())) {
            return getSubFormDataList(subFormListDTO, formDataEntity.getData(), formDataEntity, subExplainList);
        } else if (Objects.equals(FieldTypeEnum.SELECT_PRODUCT.getType(), fieldAttrEntity.getAttr())){

        }
        return null;
    }
}
